package agency.tango.materialintroscreen.interceptor;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintroscreen.interceptor
 * @ClassName: LookupTableInterpolator
 * @Description: 使用查找表根据给定的输入来计算插值
 * @CreateDate: 2021/4/12 16:18
 * @UpdateDate: 2021/4/12 16:18
 * @UpdateRemark: 暂无
 * @Version: 1.0
 */
abstract class LookupTableInterpolator implements Interpolator {
    private final float[] mValues;
    private final float mStepSize;

    protected LookupTableInterpolator(float[] values) {
        this.mValues = values;
        this.mStepSize = 1.0F / (float) (this.mValues.length - 1);
    }

    /**
     * 获取差值
     *
     * @param input A value between 0 and 1.0 indicating our current point
     *              in the animation where 0 represents the start and 1.0 represents
     *              the end
     * @return 差值
     */
    public float getInterpolation(float input) {
        return getCurvedTime(input);
    }

    @Override
    public float getCurvedTime(float input) {
        if (input >= 1.0F) {
            return 1.0F;
        } else if (input <= 0.0F) {
            return 0.0F;
        } else {
            int position =
                    Math.min((int) (input * (float) (this.mValues.length - 1)), this.mValues.length - 2);
            float quantized = (float) position * this.mStepSize;
            float diff = input - quantized;
            float weight = diff / this.mStepSize;
            return this.mValues[position] + weight * (this.mValues[position + 1] - this.mValues[position]);
        }
    }
}
